package queue;

public class MyQueueByArray {
    //数组实现队列
    public String[] arr=null;
    //队首
    public int head=0;
    //队尾
    public int tail=0;
    //元素个数
    public int size=0;
    public MyQueueByArray(){
        arr=new String[10000];
    }
    public MyQueueByArray(int capacity){
        arr=new String[capacity];
}
//入队列
    public void offer(String val){
        if (size==arr.length){
            return ;
        }else{
            //把新的元素放到tail位置
            arr[tail]=val;
            //tail后移
            tail++;
            //为构成环形队列，当tail==arr.length时，重新回到0
            if(tail==arr.length){
                tail=0;
            }
            //元素个数+1
            size++;
        }
    }
    //出队列
    public String poll() {
        if (size == 0) {
            return null;
        }
            //取队首元素
            String value = arr[head];
            //head后移
            head++;
            //为构成环形队列，当head==arr.length时，重新回到0
            if (head == arr.length) {
                head = 0;
            }
            //更新元素个数
            size--;
        return value;
    }
    //查看队首元素
    public String peek(){
        if(size==0){
            return null;
        }
        return arr[head];
    }
    //判断队列是否为空
    public Boolean isEmpty(){
        return size==0;
    }

    public static void main(String[] args) {
        MyQueueByArray queue=new MyQueueByArray();
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        System.out.println(queue.peek());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
}
